package app.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.rails.core.web.Action;
import org.rails.core.web.ControllerBase;
import org.rails.extjs.AbsBase;
import org.rails.extjs.ExtJS;
import org.rails.extjs.ajax.SimpleRequest;
import org.rails.extjs.button.Button;
import org.rails.extjs.calendar.CalendarPanel;
import org.rails.extjs.calendar.EventStore;
import org.rails.extjs.data.MemoryProxy;
import org.rails.extjs.javascript.Function;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.json.Json;
import org.rails.extjs.layout.FitLayout;
import org.rails.extjs.panel.Panel;
import org.rails.extjs.panel.Viewport;
import org.rails.extjs.panel.Window;
import org.rails.extjs.Listener;

public class CalendarController extends ControllerBase {

	public CalendarController(Action action) throws Exception {
		super(action);
	}
	
	public void indexAction() throws IOException, ServletException{
		AbsBase.setDebug(true);
		Viewport view = new Viewport();
		view.setLayout(new FitLayout());
		Panel body = new Panel();
		body.setLayout(new FitLayout());
		CalendarPanel cp = new CalendarPanel();
//		cp.setActiveItem(2);
		cp.setBorder(false);
//		cp.setTitle("Calendar");

		EventStore store = new EventStore();
		store.setStoreId("Store123");
		Json json = new Json();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("id",1);
		data.put("cid", 1);
		data.put("title","this test");
		data.put("start","2010-10-13");
		data.put("end","2010-10-20");
		data.put("ad",false);
		data.put("rem","15");
		data.put("notes", "this notes...");
		list.add(data);
		
		data = new HashMap<String,Object>();
		data.put("id",2);
		data.put("cid", 1);
		data.put("title","this testdddd");
		data.put("start","2010-10-01");
		data.put("end","2010-10-03");
		data.put("ad",false);
//		data.put("rem","15");
		data.put("notes", "this notes.efdsfdsf..");
		list.add(data);
		
		data = new HashMap<String,Object>();
		data.put("id",4);
		data.put("cid", 1);
		data.put("title","this testdddd");
		data.put("start","2010-10-28");
		data.put("end","2010-10-29");
		data.put("ad",false);
//		data.put("rem","15");
		data.put("notes", "this notes.efdsfdsf..");
		list.add(data);
		
		data = new HashMap<String,Object>();
		data.put("id",3);
		data.put("cid", 1);
		data.put("title","this testdfdgdgddd");
		data.put("start","2010-10-08");
		data.put("end","2010-10-10");
		data.put("ad",false);
//		data.put("rem","17");
		data.put("notes", "this notes.efdsfdsf..");
		list.add(data);
		
		json.put("evts",list);
		store.setData(json);
		store.setRoot("evts");
//		store.setIdProperty("id");
		store.setProxy(new MemoryProxy());
		cp.setEventStore(store);
		cp.setMonthViewCfg(true, true, true);

//		cp.setShowNavBar(false);
//		cp.setShowWeekView(false);
		JavaScript js = new JavaScript();
		SimpleRequest req = new SimpleRequest("../Calendar/test.action");
	
		js.append(req.getJavaScript());
//		js.append("Ext.getCmp('Win123').on('close', onComplete, this, {single:true});");
//		cp.addListener(new Listener("eventclick",new Function(new JavaScript("alert('eventclick');"),"vw","rec","el")));
//		cp.addListener(new Listener("eventout",new Function(new JavaScript("alert('eventout');"),"vw","rec","el")));
//		cp.addListener(new Listener("eventadd",new Function(new JavaScript("alert('eventadd');"),"cp","rec")));
		cp.addListener(new Listener("dayclick",new Function("alert(win);","win,dates,onComplete")));//Ext.select('.ext-del-btn').setDisplayed(false);
		//Ext.select('.ext-dd-shim').unselectable();
		
//		cp.addListener(new Listener("rangeselect",new Function(new JavaScript("showEditWindow(dates);editWin.on('hide', onComplete, this, {single:true});"),"win", "dates", "onComplete")));
		cp.addListener(new Listener("rangeselect",new Function(
				js,"win,dates,onComplete")));
		
//		cp.addListener(new Listener("viewchange",new Function(
//				new JavaScript("alert(p.getId());"),"p", "vw", "dataInfo")));
		cp.setShowDayView(true);
		
		body.addItem(cp);
		view.addItem(body);
		ExtJS.bind(request, view);
		action.render();
	}
	
	public void testAction() throws IOException{
		final Json json = new Json();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("id",1);
		data.put("cid", 1);
		data.put("title","this test");
		data.put("start","2010-10-13");
		data.put("end","2010-10-20");
		data.put("ad",false);
		data.put("rem","15");
		data.put("notes", "this notes...");
		list.add(data);
		json.put("evts", list);
		Window win = new Window();
		win.setCloseAction(Window.CLOSE_ACTION_CLOSE);
		win.setId("Win123");
		win.setWidth(500);
		win.setHeight(400);
		win.setLayout(new FitLayout());
		
		win.addButton(new Button("test","",new Function(new JavaScript("Ext.StoreMgr.lookup('{0}').loadData({1});","Store123",json.createJson()))));
		win.addListener(new Listener("show",new Function(new JavaScript("this.on('close', onComplete);"))));
		ExtJS.bind(request, win);
		ExtJS.sendToClient(request, action.out());
	}

}
